package com.shigu.route;

import com.opentae.core.mybatis.utils.FieldUtil;
import com.opentae.data.mall.beans.MoPassageway;
import com.opentae.data.mall.beans.MoPassagewayRoute;
import com.opentae.data.mall.examples.MoPassagewayRouteExample;
import com.opentae.data.mall.interfaces.MoPassagewayMapper;
import com.opentae.data.mall.interfaces.MoPassagewayRouteMapper;
import com.shigu.passageway.Passageway;
import com.shigu.passageway.bo.RegistBO;
import com.shigu.passageway.exceptions.PassagewayException;
import com.shigu.route.bo.WaySelBO;
import com.shigu.route.vo.RouteRuleVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DefaultPassagewayRoute implements PassagewayRoute{

    private Map<String,RouteRuleVO> bankMap;//按银行限制的通道

    @Autowired
    private Map<String,Passageway> passageways;

    private final String defaultPassageway="yeepayPassageway";

    @Autowired
    private MoPassagewayRouteMapper moPassagewayRouteMapper;

    @Autowired
    private MoPassagewayMapper moPassagewayMapper;

    @PostConstruct
    @Override
    public void init() {
        bankMap=new HashMap<>();
        List<MoPassagewayRoute> routes=moPassagewayRouteMapper.selectByExample(new MoPassagewayRouteExample());
        for(MoPassagewayRoute r:routes){
            switch (r.getType()){
                case 1:bankMap.put(r.getFlagValue(),new RouteRuleVO(r.getPassagewayName(),
                        Double.valueOf(r.getRate())));break;
            }
        }
    }

    /**
     * 权重顺序
     * 1通过刷卡行，2通过金额，3通过代理商，4通过刷卡人，5随机通道
     * @param param
     * @return
     */
    @Override
    public Passageway selway(WaySelBO param) {
        Passageway result=selwayByBank(param.getBankName());
        if (result != null) {
            return result;
        }
        result=selwayByMoney(param.getMoney());
        if (result != null) {
            return result;
        }
        result=selwayByAgent(param.getAgentId());
        if (result != null) {
            return result;
        }
        result=selwayByBusiness(param.getBusinessId());
        if (result != null) {
            return result;
        }
        return defaultway();
    }

    @Override
    public void registAllway(RegistBO bo) throws PassagewayException {
        for (Passageway way:passageways.values()){
            way.regist(bo);
        }
    }

    @Override
    public void updateAllway(RegistBO bo) throws PassagewayException {
        for (Passageway way:passageways.values()){
            way.updateInfo(bo);
        }
    }

    @Override
    public void setAllFee(Long agentId, Long businessId, Double rate) throws PassagewayException {
        for (Passageway way:passageways.values()){
            way.setFee(agentId,businessId,rate,null);
        }
    }

    @Override
    public void setAgentFee(Long agentId, Double rate) throws PassagewayException {
        for (Passageway way:passageways.values()){
            way.superSetFee(agentId,rate,way.getSystemRate(),null);
        }
    }

    @Override
    public Passageway selwayByBank(String bankName) {
        if (StringUtils.isEmpty(bankName)) {
            return null;
        }
        RouteRuleVO vo=bankMap.get(bankName);
        if (vo == null||!randomRate(vo.getRate())) {//没取到或者没达到爆点，返回null跳走
            return null;
        }
        return passageways.get(vo.getPassagewayName());
    }

    @Override
    public Passageway selwayByMoney(Double money) {
        if (money == null) {
            return null;
        }
        return null;
    }

    @Override
    public Passageway selwayByAgent(Long agentId) {
        if (agentId == null) {
            return null;
        }
        return null;
    }

    @Override
    public Passageway selwayByBusiness(Long businessId) {
        if (businessId == null) {//is ok?
            return null;
        }
        return null;
    }

    @Override
    public Passageway defaultway() {
        return passageways.get(defaultPassageway);
    }

    @Override
    public Passageway selwayByPid(Long pid) {
        MoPassageway mp=moPassagewayMapper.selectFieldsByPrimaryKey(pid, FieldUtil.codeFields("tag_name"));
        if (mp == null) {
            return null;
        }
        return selwayByTagName(mp.getTagName());
    }

    @Override
    public Passageway selwayByTagName(String tagName) {
        return passageways.get(tagName);
    }

    /**
     * 按概率出现
     * @param rate
     * @return
     */
    private boolean randomRate(Double rate){
        if (rate==1d) {
            return true;
        }
        return Math.random()<rate;
    }
}
